package com.beidouapp.security.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

import com.beidouapp.common.enums.RoleGroupType;
import com.beidouapp.domain.pojo.Resource;
import com.beidouapp.security.model.MenuVo;
import com.beidouapp.security.model.SessionUser;
import com.beidouapp.security.model.SubMenu;
import com.beidouapp.security.mvc.constant.SessionConstants;

public abstract class DscpCommUtils {
    public static final String SUPPER_ADMIN = "beidouadmin";
    private static final String RESOURCE_MENU_TYPE_CODE = "WebMenu";
    
    public static SessionUser getSessionUser(HttpSession session) {
        SessionUser user = (SessionUser) session.getAttribute(SessionConstants.SESSION_DSCP_USER_OBJECT);
        return user;
    }

    public static void setSessionUser(HttpSession session, SessionUser sessionUser) {
        session.setAttribute(SessionConstants.SESSION_DSCP_USER_OBJECT, sessionUser);
    }

    public static boolean isAdmin(HttpSession session) {
        SessionUser user = (SessionUser) session.getAttribute(SessionConstants.SESSION_DSCP_USER_OBJECT);
        if (user != null) {
            if (user.getLoginname().equals(SUPPER_ADMIN)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isSupperAdmin(HttpSession session) {
        SessionUser user = (SessionUser) session.getAttribute(SessionConstants.SESSION_DSCP_USER_OBJECT);
        if (user != null) {
            if (user.getLoginname().equals(SUPPER_ADMIN)) {
                return true;
            }
            if (user.getRoleGroupId() == RoleGroupType.PLAT_ROLE.getCode()) {
                return true;
            }
        }
        return false;
    }

    public static List<String> getResource(HttpSession session) {
        List<String> resources = new ArrayList<String>();
        SessionUser sessionUser = (SessionUser) DscpCommUtils.getSessionUser(session);
        if (sessionUser != null) {
            resources = sessionUser.getResourceCodes();
        }
        return resources;
    }

    public static boolean isManagerPlat(HttpSession session) {
        SessionUser sessionUser = (SessionUser) DscpCommUtils.getSessionUser(session);
        if (RoleGroupType.PLAT_ROLE.getCode() == sessionUser.getRoleGroupId()) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    public static List<MenuVo> assembleMenu(List<Resource> resources) {
        Map<Long, TreeMap<Long, SubMenu>> subTreesMap = new HashMap<Long, TreeMap<Long, SubMenu>>();
        /* TreeMap<Long,Menu> rootTree = new TreeMap<Long,Menu>(); */
        TreeMap<Long, MenuVo> rootTree = new TreeMap<Long, MenuVo>();
        for (Resource resource : resources) {
            if (!resource.getResourceTypeCode().equals(RESOURCE_MENU_TYPE_CODE)) {
                continue;
            }
            Long id = resource.getId();
            String code = resource.getCode();
            String name = resource.getName();
            String url = resource.getUrl();
            Integer seq = resource.getOrdinal();
            Long parentId = resource.getParentId();
            String mainPic = resource.getMainPic();
            // 0xff(序号) 0x80000000(id值)
            long key = id.longValue() | 0xff80000000L;
            if (seq != null) {
                key = (seq.longValue() << 32 | 0x007fffffffL) & key;
            }
            if (parentId == null) {
                // Menu muenu = new Menu();
                MenuVo muenu = new MenuVo();
                muenu.setId(id);
                muenu.setCode(code);
                muenu.setName(name);
                muenu.setUrl(url);
                muenu.setMainPic(mainPic);
                rootTree.put(key, muenu);
            } else {
                long parentkey = parentId.longValue();
                TreeMap<Long, SubMenu> subTree = null;
                if (!subTreesMap.containsKey(parentkey)) {
                    subTree = new TreeMap<Long, SubMenu>();
                    subTreesMap.put(parentkey, subTree);
                } else {
                    subTree = subTreesMap.get(parentkey);
                }
                SubMenu submenu1 = new SubMenu(id, code, name, url);
                subTree.put(key, submenu1);
            }
        }
        // List<Menu> topmenus = new ArrayList<Menu>();
        List<MenuVo> topmenus = new ArrayList<MenuVo>();
        /* for(Map.Entry<Long, Menu> entry : rootTree.entrySet()){ */
        for (Map.Entry<Long, MenuVo> entry : rootTree.entrySet()) {
			/* Menu menu = entry.getValue(); */
            MenuVo menu = entry.getValue();
            TreeMap<Long, SubMenu> subTree = subTreesMap.get(Long.valueOf(menu.getId()));
            if (subTree != null) {
                List<SubMenu> subMenuList = new ArrayList<SubMenu>();
                subMenuList.addAll(subTree.values());
                menu.setSubmenu(subMenuList);
            }
            topmenus.add(menu);
        }
        return topmenus;
    }

    public static List<String> toResourceList(List<Resource> resources) {
        List<String> list = new ArrayList<String>();
        for (Resource resource : resources) {
            list.add(resource.getCode());
        }
        return list;
    }

    public static Map<String, Resource> toResourceMap(List<Resource> resources) {
        Map<String, Resource> map = new HashMap<String, Resource>();
        for (Resource resource : resources) {
            map.put(resource.getCode(), resource);
        }
        return map;
    }

    public static List<Resource> getResourcesByCodes(List<String> codes, List<Resource> fullResources) {
        List<Resource> resources = new ArrayList<Resource>();
        Map<String, Resource> resourceMap = toResourceMap(fullResources);
        for (String code : codes) {
            Resource resource = resourceMap.get(code);
            if (resource != null) resources.add(resource);
        }
        return resources;
    }

    public static List<Resource> getResourcesByCodes(List<String> codes, Map<String, Resource> fullResourcesMap) {
        List<Resource> resources = new ArrayList<Resource>();
        for (String code : codes) {
            Resource resource = fullResourcesMap.get(code);
            if (resource != null) resources.add(resource);
        }
        return resources;
    }


}
